package cih;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cci.CtrlInterno;
import cci.CtrlPrincipal;
import cdp.Localizacao;
import cdp.Pessoa;
import cdp.Usuario;

@WebServlet(name = "PagPrincipal", urlPatterns = {"/PagPrincipal"})
public class PagPrincipal extends HttpServlet{
	CtrlPrincipal ctrl = CtrlPrincipal.getInstance();
	CtrlInterno ctrlInterno = CtrlInterno.getInstance();
	
	HttpServletRequest request;
	HttpServletResponse response;
	RequestDispatcher dispatcher;
	HttpSession session;
	private String opcao;
	private String opcaoCoordenadas;
	
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.request = request;
		this.response = response;
		this.session = request.getSession();

		System.out.println("Entrou no servlet...");       
		
		this.opcao = (String) request.getParameter("opcao");
		this.opcaoCoordenadas = (String) request.getParameter("opcaoCoordenadas");
		System.out.println("opcao: "+ opcao);
		System.out.println("opcaoCoordenadas: "+ opcaoCoordenadas);
		
		if (opcao != null)
		{				
			switch(opcao)
			{
				case "logoff": 
					this.encerrarSessao(); 
					break;
				case "alterar": 
					request.getRequestDispatcher("PagAlterarCadastro.jsp").forward(request, response); 
					break;

								
				default: 
					break;
			}
		}
		else if (opcaoCoordenadas != null) {			
			switch(opcaoCoordenadas)
			{
				case "salvarPosicao":
					this.salvarPosicao();
					break;
				case "mostrarCoordenadas": 
					this.mostrarCoordenadas();
					break;
				case "opcaoDatas":
					String data;
					String dataI = request.getParameter("dataI");
					dataI = montaData(dataI);
					//String [] dataIS = data.matches("\\d{2}/\\d{2}/\\d{4}")? data.split("/"): null;
					String dataF = request.getParameter("dataF");
					//String [] dataFS = data.matches("\\d{2}/\\d{2}/\\d{4}")? data.split("/"): null;
					dataF = montaData(dataF);
					
					this.mostrarLocalizacaoPorData(dataI, dataF);
					request.getRequestDispatcher("PagPrincipal.jsp").forward(request, response); 
					break;
				
								
				default: break;
			}
		}
    }
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {   
        processRequest(request, response);
    }
	
	public void encerrarSessao()
	{		
		try {
			session.setAttribute("usuario", null);
			response.sendRedirect("index.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void salvarPosicao()
	{
		System.out.println("entrou no salvar!!");
		
		Usuario u;
		Pessoa p;
		
		ArrayList<Object> lista = new ArrayList<>(); 
				
		try {
			u = (Usuario) session.getAttribute("usuario");
			p = ctrlInterno.obterPessoa(u);
			
			lista.add(request.getParameter("txtLatitude"));
			lista.add(request.getParameter("txtLongitude"));
			lista.add(request.getParameter("legenda"));
			lista.add(p);
			
			ctrlInterno.incluirLocalizacao(lista);
			
			response.sendRedirect("PagPrincipal.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void mostrarCoordenadas()
	{
		Usuario u;
		Pessoa p;
		
		u = (Usuario) session.getAttribute("usuario");
		p = ctrlInterno.obterPessoa(u);
		
		List <Localizacao> l = ctrlInterno.obterLocalizacoes(p);
		
		request.setAttribute("coordenadas", l);
	}
	
	public void mostrarLocalizacaoPorData(String dataI, String dataF){
		Usuario u;
		Pessoa p;
		
		u = (Usuario) session.getAttribute("usuario");
		p = ctrlInterno.obterPessoa(u);
		
		List <Localizacao> locals = ctrlInterno.obterLocalizacoes(p, dataI, dataF);
		System.out.println(locals.size());
		
		request.setAttribute("localizacoes", locals);
	}
	
	public String montaData(String data)
	{
		String nova = "";
		nova+= data.substring(6, 10)+"-";
        nova+= data.substring(3, 5)+"-";
        nova+=data.substring(0, 2);
		return nova;
	}
}